package com.jinhua.medical.manager.service;

import com.jinhua.medical.manager.mybatis.dto.BackUser;
import com.jinhua.medical.manager.mybatis.dto.BackUserExample;
import com.jinhua.medical.manager.mybatis.mapper.BackUserMapper;
import com.jinhua.medical.manager.util.IDGenerator;
import com.jinhua.medical.manager.util.MD5Generator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户业务对象
 * Created by 王兴光 on 2018/7/5.
 */
@Service("userService")
public class BackUserService {
    @Resource
    private BackUserMapper userMapper;

    /**
     * 根据用户名与密码获取用户信息
     * @param userName      用户名
     * @param password      密码
     * @return              用户信息
     */
    public BackUser getCurrentUser(String userName, String password){
        BackUser user = null;
        BackUserExample example = new BackUserExample();
        example.getOredCriteria().add(example.createCriteria().andUserNameEqualTo(userName).andPasswordEqualTo(MD5Generator.EncoderByMd5(password)));
        if(userMapper.selectByExample(example).size()>0){
            user = userMapper.selectByExample(example).get(0);
        }
        return user;
    }

    /**
     * 添加用户信息
     * @param userName      用户名
     * @param password      密码
     * @param roleId        角色编号
     */
    @Transactional
    public void addUserInfo(String userName,String password,int roleId) {
        BackUser user = new BackUser();
        user.setId(IDGenerator.uuid());
        user.setUserName(userName);
        user.setPassword(MD5Generator.EncoderByMd5(password));
        user.setRoleId(roleId);
        userMapper.insertSelective(user);
    }

    public static void main(String[] args){
        /*ApplicationContext ac = new ClassPathXmlApplicationContext("spring/spring-*.xml");
        BackUserService service = (BackUserService)ac.getBean("userService");
        service.addUserInfo("admin","Zheng02mysql",1);*/
        System.out.println(MD5Generator.EncoderByMd5("Zheng02mysql"));
    }
}
